home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 351-375 / 351 / pdc / pdcsrc.lzh / PDC / MemMgt.c < prev    next >
C/C++ Source or Header  |  1990-04-06  |  5KB  |  200 lines

  1.  
  2. /* PDC Compiler - A Freely Distributable C Compiler for the Amiga
  3.  *                Based upon prior work by Matthew Brandt and Jeff Lydiatt.
  4.  *
  5.  * PDC Compiler release 3.3 Copyright (C) 1989 Paul Petersen and Lionel Hummel.
  6.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  7.  *
  8.  * This code is freely redistributable upon the conditions that this 
  9.  * notice remains intact and that modified versions of this file not be 
  10.  * distributed as part of the PDC Software Distribution without the express
  11.  * consent of the copyright holders.
  12.  *
  13.  *------------------------------------------------------------------
  14.  *
  15.  * $Log:    MemMgt.c,v $
  16.  * Revision 3.33  90/04/05  22:40:18  lionel
  17.  * Hid a null-loop in xalloc to sanity check entire global list.
  18.  * 
  19.  * Revision 3.32  90/02/03  16:24:55  lionel
  20.  * None
  21.  * 
  22.  *------------------------------------------------------------------
  23.  */
  24.  
  25. /*
  26.  * Memmgt.c - Handles efficient and clean allocation of blocks of memory -
  27.  * Cleanly deallocates local and global symbol entries
  28.  */
  29.  
  30. #include        <stdio.h>
  31. #include        "C.h"
  32. #include        "Expr.h"
  33. #include        "Gen.h"
  34. #include        "Cglbdec.h"
  35.  
  36. #ifdef __PDC__
  37. #ifdef unix
  38. #define free    xfree    /* We do not presently mess with block deallocation */
  39. #define calloc  xcalloc
  40. #endif
  41. #endif
  42.  
  43. extern char    *itoa();
  44. extern char    *calloc();
  45. extern char    *malloc();
  46.  
  47. struct blk {
  48.     struct blk     *next;
  49.     char            m[1];   /* memory area */
  50. };
  51.  
  52. long            glbsize = 0,    /* size left in current global block    */
  53.                 locsize = 0,    /* size left in current local block     */
  54.                 glbindx = 0,    /* global index         */
  55.                 locindx = 0;    /* local index          */
  56.  
  57. long            glbwaste = 0;   /* Unused memory at the end of the block */
  58. long            locwaste = 0;   /* Unused memory at the end of the block */
  59.  
  60. long            glballoc = 0;   /* Number of times calloc is called     */
  61. long            localloc = 0;   /* Number of times calloc is called     */
  62.  
  63. struct blk     *locblk = NULL,  /* pointer to local block       */
  64.                *glbblk = NULL;  /* pointer to global block      */
  65.  
  66. char           *
  67. xalloc(siz)
  68.     int             siz;
  69. {
  70.     struct blk     *bp;
  71.     char           *rv, *mem;
  72.     extern char    *calloc();
  73.  
  74.     if (siz & 1)        /* if odd size */
  75.         siz += 1;   /* make it even */
  76.  
  77.     if (siz > 2048) {
  78.         fprintf( stderr, "DIAG -- xalloc, size greater than 2048\n" );
  79.         exit(1);
  80.     }
  81.  
  82.     if (global_flag) {
  83.         if (glbsize > siz) {
  84.             rv = &(glbblk->m[glbindx]);
  85.             glbsize -= siz;
  86.             glbindx += siz;
  87.             mem = (char *) rv;
  88. #if 0
  89. for (bp = glbblk; bp; bp = bp->next);
  90. #endif
  91.             goto done;
  92.         }
  93.         else {
  94.             glbwaste += glbsize;
  95.             ++glballoc;
  96.             bp = (struct blk *) malloc( sizeof(struct blk) + 2047 );
  97.             if (bp == NULL) {
  98.                 fprintf( stderr, " not enough memory.\n" );
  99.                 exit(1);
  100.             }
  101.             bzero( bp, sizeof(struct blk) + 2047 );
  102.             bp->next = glbblk;
  103.             glbblk = bp;
  104.             glbsize = 2048 - siz;
  105.             glbindx = siz;
  106.             mem = (char *) glbblk->m;
  107.             goto done;
  108.         }
  109.     }
  110.     else {
  111.         if (locsize > siz) {
  112.             rv = &(locblk->m[locindx]);
  113.             locsize -= siz;
  114.             locindx += siz;
  115.             mem = (char *) rv;
  116.             goto done;
  117.         }
  118.         else {
  119.             locwaste += locsize;
  120.             ++localloc;
  121.             bp = (struct blk *) malloc( sizeof(struct blk) + 2047 );
  122.             if (bp == NULL) {
  123.                 fprintf( stderr, " not enough local memory.\n" );
  124.                 exit(1);
  125.             }
  126.             bzero( bp, sizeof(struct blk) + 2047 );
  127.             bp->next = locblk;
  128.             locblk = bp;
  129.             locsize = 2048 - siz;
  130.             locindx = siz;
  131.             mem = (char *) locblk->m;
  132.             goto done;
  133.         }
  134.     }
  135. done:
  136.     if (mem == NULL) {
  137.         fprintf( stderr, "DIAG -- NULL responce from xalloc\n" );
  138.     }
  139.     return (mem);
  140. }
  141.  
  142.  
  143. void
  144. release_local()
  145. {
  146.     struct blk     *bp1, *bp2;
  147.     char           *nbuf;
  148.     int             blkcnt;
  149.  
  150.     locwaste += locsize;
  151.  
  152.     blkcnt = 0;
  153.     bp1 = locblk;
  154.     while (bp1 != NULL) {
  155.         bp2 = bp1->next;
  156.         free(bp1);
  157.         ++blkcnt;
  158.         bp1 = bp2;
  159.     }
  160.     locblk = 0;
  161.     locsize = 0;
  162.     lsyms.head = NULL;
  163.  
  164.     if (!Options.Quiet) {
  165.         if (lastfunc != NULL && lastfunc->name != NULL)
  166.             nbuf = lastfunc->name;
  167.         else
  168.             nbuf = "**PDC**";
  169.         fprintf( stderr, "%s : %d bytes local tables.\n", 
  170.                          nbuf, blkcnt * 2048L );
  171.     }
  172. }
  173.  
  174. void
  175. release_global()
  176. {
  177.     struct blk     *bp1, *bp2;
  178.     int             blkcnt;
  179.  
  180.     glbwaste += glbsize;
  181.  
  182.     bp1 = glbblk;
  183.     blkcnt = 0;
  184.     while (bp1 != NULL) {
  185.         bp2 = bp1->next;
  186.         free(bp1);
  187.         ++blkcnt;
  188.         bp1 = bp2;
  189.     }
  190.     glbblk = 0;
  191.     glbsize = 0;
  192.     gsyms.head = NULL;  /* clear global symbol table */
  193.     strtab = NULL;      /* clear literal table */
  194.  
  195.     if (!Options.Quiet) {
  196.         fprintf( stderr, " releasing %d bytes global tables\n", 
  197.                          blkcnt * 2048L );
  198.     }
  199. }
  200.